package my.example.samplegridview;

import java.util.Vector;

import my.example.samplegridview.modal.review;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

public class viewreview extends Activity{
	
	private Context mContext;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.viewreview);
        
        
        mContext = this.getBaseContext();
        Intent i = getIntent();
        
        Vector<review> reviewlist = datainitialization(mContext,i.getStringExtra("product_id"));
        
        if(reviewlist.size() == 0){
        	Toast.makeText(this, "No reviews available for this product.", Toast.LENGTH_SHORT).show();
        	finish();
        }
        else{
        	ListView viewReviewList = (ListView) findViewById(R.id.viewreviewreviewlist);
            viewReviewList.setAdapter(new viewreviewadapter(this,reviewlist));
        }
        
        Button backbutton = (Button) findViewById(R.id.viewreviewback);
        backbutton.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				viewreview.this.finish();
			}
        	
        });
	}
	
	public static Vector<review> datainitialization(Context context ,String productid){
		Vector<review> reviewlist = new Vector<review>();
		String[] args = {productid};
    	Cursor c = context.getContentResolver().query(review.CONTENT_URI, null, "product_id=?",args, null);
    	while(c.moveToNext()){
    		review r = new review();
    		r.setReview_id(String.valueOf(c.getInt(0)));
    		r.setProduct_id(productid);
    		r.setCustomer_id(String.valueOf(c.getInt(2)));
    		r.setAuthor(c.getString(3));
    		r.setText(c.getString(4));
    		r.setRating(c.getInt(5));
    		r.setStatus(c.getInt(6));
    		r.setDate_added(c.getString(7));
    		r.setDate_modified(c.getString(8));
    		reviewlist.add(r);
    	}
    	c.close();
		return reviewlist;
	}
	
	public class viewreviewadapter extends BaseAdapter{

		Context mContext;
		Vector<review> reviewlist;
		
		public viewreviewadapter(Context mContext,Vector<review> reviewlist){
			this.mContext = mContext;
			this.reviewlist = reviewlist;
		}
		
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return this.reviewlist.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return this.reviewlist.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			final review r = reviewlist.get(position);

			LinearLayout reviewView = null;
			if (convertView == null) { // if it's not recycled, initialize some
										// attributes
				LayoutInflater li = (LayoutInflater) mContext
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				reviewView = (LinearLayout) li
						.inflate(R.layout.viewreviewitem, null);
			} else {
				reviewView = (LinearLayout) convertView;
			}
			
			TextView tv = (TextView) reviewView.findViewById(R.id.viewreviewitemauthorname);
			tv.setText(r.getAuthor());
			
			tv = (TextView) reviewView.findViewById(R.id.viewreviewitemdateadded);
			tv.setText(r.getDate_added());
			
			RatingBar rb = (RatingBar) reviewView.findViewById(R.id.viewreviewitemrating);
			rb.setRating(r.getRating());
			
			tv = (TextView) reviewView.findViewById(R.id.viewreviewitemreview);
			tv.setText(r.getText());
			
			return reviewView;
		}
		
	}
}
